import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { HttpService } from '../service/http.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
})
export class LoginComponent {
  constructor(
    private router: Router,
    private httpService: HttpService,
    private modalService: NzMessageService,
    private notification: NzNotificationService,
  ) {}

  public loginInfo: any = {
    name: '',
    password: '',
  };
  public timer: any;
  login() {
    const url = '/rest/login';
    const data = {
      password: this.loginInfo.password,
      name: this.loginInfo.name,
    };
    return this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code != '0000') {
        this.modalService.error('密码错误');
      } else {
        localStorage.setItem('user', JSON.stringify(res.data));
        this.router.navigate(['/loglist']);
        const d = res.data.lastLogin + '';
        const d2 = d.replace('T', ' ');
        const welcomeTime = '上次登录时间: ' + d2.substring(0, 19) + '.<br>';
        const welcomeIP =
          welcomeTime + '上次登录IP地址: ' + res.data.lastLoginIP + '.<br>';
        const welcomeSay = welcomeIP + '每日一语: ' + res.data.saying;

        this.timer = setTimeout(() => {
          this.notification.blank('欢迎使用宣杰系统', welcomeSay, {
            nzPlacement: 'bottomRight',
            nzDuration: 10000,
          });
        }, 1000);
      }
    });
  }
}
